<script setup lang="ts">
import { reactive,ref } from "vue";

const form = reactive({
  phone: "18784720421",
  password: "111111",
});
function onSubmit() {
  froRef.value?.validate().catch((err) => { //如果验证不通过
    ElMessage.error("表单校验失败");
  });
}
//定义表单验证
import type { FormInstance, FormRules } from "element-plus"; //引入表单验证规则
import { ca } from "element-plus/es/locales.mjs";
const rules = reactive<FormRules>({
  phone: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{8}$/,
      message: "请输入正确的手机号",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 6, max: 18, message: "密码6-18位", trigger: "blur" },
  ],
});
const froRef = ref<FormInstance>()
</script>

<template>
  <div class="login">
    <el-form
      ref="froRef"
      :rules="rules"
      :model="form"
      label-width="auto"
      style="max-width: 600px"
      label-position="top"
      size="large"
    >
      <h2>登陆界面</h2>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  background-color: #ddd;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-form {
    padding: 30px;
    width: 300px;
    background-color: #ffffff;
    border-radius: 10px;

    .el-form-item {
      margin-top: 20px;
    }

    .el-button {
      width: 100%;
      margin-top: 30px;
    }
  }
}
</style>
